package com.bysj.lms.controller;


import com.bysj.lms.common.ResultModel;
import com.bysj.lms.pojo.dto.LmsHasDto;
import com.bysj.lms.pojo.entity.Cabinetinfo;
import com.bysj.lms.pojo.entity.LmsHas;
import com.bysj.lms.service.ICabinetinfoService;
import com.bysj.lms.service.ILmsHasService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jdk.nashorn.internal.objects.annotations.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiaojiakai
 * @since 2022-02-04
 */

@Api(tags = "服务器托管管理")
@RestController
@RequestMapping("/lms-has")
public class LmsHasController {

    @Autowired
    ILmsHasService lmsHasService;
    @Autowired
    ICabinetinfoService cabinetinfoService;

    @ApiOperation("得到托管模型")
    @GetMapping()
    public LmsHas getModel(){
        return new LmsHas();
    }

    @ApiOperation(value = "申请托管")
    @PostMapping("/apply")
    public ResultModel apply(@RequestBody LmsHas lmsHas){

        boolean save = lmsHasService.save(lmsHas);
        if(save) return ResultModel.success("申请成功");
        return ResultModel.failure("申请失败");
    }

    @ApiOperation("审批")
    @PutMapping("/handle")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "chargerId",value = "审批人id"),
            @ApiImplicitParam(name = "applyId",value = "申请表id")
    })
    public ResultModel handle(String chargerId,String applyId,String status){
        boolean handle = lmsHasService.handle(chargerId,applyId, status);
        if(handle) return ResultModel.success("操作成功");
        return ResultModel.failure("操作失败");
    }

    @ApiOperation("查看申请表")
    @GetMapping("/find")
    public ResultModel find(String uid,String name,String status,Integer count,Integer page){
        PageInfo<LmsHasDto> pageInfo = lmsHasService.findByPage(uid,name,status, page, count);
        return ResultModel.success("查询成功",pageInfo);
    }

    @ApiOperation("取出")
    @DeleteMapping("/delete")
    public ResultModel delete(String id,Integer cabinetinfoId){
        boolean b = lmsHasService.removeById(id);
        cabinetinfoService.lambdaUpdate().eq(Cabinetinfo::getId,cabinetinfoId).set(Cabinetinfo::getStatus,"0").update();
        return b?ResultModel.success("取出成功"):ResultModel.failure("取出失败");
    }

//    @ApiOperation("修改")
//    @PutMapping("/edit")
//    public ResultModel edit(@RequestBody LmsHas lmsHas){
//        LmsHas one = lmsHasService.getById(lmsHas.getId());
//
//    }

    @ApiOperation(value = "完成托管",notes = "绑定机柜")
    @PutMapping("/trusteeship")
    public ResultModel trusteeship(@RequestBody LmsHas lmsHas){
        boolean trusteeship = lmsHasService.trusteeship(lmsHas);
        return trusteeship?ResultModel.success():ResultModel.failure();
    }

}

